package com.demo.lucene.solr.controller;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lee9213@163.com
 * @version 1.0
 * @date 2017-04-04 21:27
 */
@RestController
@RequestMapping("solr")
public class SolrController {

    private SolrClient solrClient;

    public SolrController(SolrClient solrClient) {
        this.solrClient = solrClient;
    }

    @RequestMapping("select")
    public void solr() throws IOException, SolrServerException {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("id:123");
        QueryResponse response = solrClient.query(solrQuery);
        SolrDocumentList documents = response.getResults();
        for(SolrDocument document : documents){
            System.out.println(document.getFieldValue("id"));
            System.out.println(document.getFieldValue("title"));
        }

    }
}
